package com.sgb.kjwl.view.ui.fragment.enterprise;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentPatentAlreadyPublishBinding;
import com.sgb.kjwl.model.entity.enterprise.PublishPatentDetailEntity;
import com.sgb.kjwl.utils.ActivityJump;
import com.sgb.kjwl.utils.DialogFunction;
import com.sgb.kjwl.utils.DialogHelper;
import com.sgb.kjwl.view.ui.activity.enterprise.MakeOverPublishActivity;
import com.sgb.kjwl.viewmodel.enterprise.PublishViewModel;

public class PatentAlreadyPublishFragment extends AppFragment {

    FragmentPatentAlreadyPublishBinding mBinding;
    PublishViewModel mViewModel;
    PublishPatentDetailEntity mEntity;

    public static PatentAlreadyPublishFragment newInstance(long id) {
        PatentAlreadyPublishFragment tradePublishFragment = new PatentAlreadyPublishFragment();
        Bundle bundle = new Bundle();
        bundle.putLong("id", id);
        tradePublishFragment.setArguments(bundle);
        return tradePublishFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_patent_already_publish, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mBinding.setLifecycleOwner(this);
        mViewModel = new ViewModelProvider(this).get(PublishViewModel.class);
        long id = getArguments().getLong("id");
        mBinding.setViewModel(mViewModel);
        mBinding.setContext(getContext());

        mViewModel.getPatentPublishDetail(getContext(), id + "", 2);

        mViewModel.mPublishPatentDetailLiveData.observe(getViewLifecycleOwner(), new Observer<PublishPatentDetailEntity>() {
            @Override
            public void onChanged(PublishPatentDetailEntity publishPatentDetailEntity) {
                mBinding.idRootLayout.setVisibility(View.VISIBLE);
                mBinding.setC(publishPatentDetailEntity);
                mEntity = publishPatentDetailEntity;
                //在售中
                if (null == mEntity.getOrderStatus() || 3 == mEntity.getOrderStatus()) {
                    mBinding.tvMessagetype.setVisibility(1 == mEntity.getStatus() ? View.VISIBLE : View.GONE);
                    mBinding.llBottom.setVisibility(0 == mEntity.getStatus() ? View.VISIBLE : View.GONE);
                } else if (1 == mEntity.getOrderStatus() || 2 == mEntity.getOrderStatus() || 4 == mEntity.getOrderStatus()) {
                    //交易中
                    mBinding.llBottom.setVisibility(View.GONE);
                    mBinding.tvMessagetype.setVisibility(View.GONE);
                } else if (5 == mEntity.getOrderStatus()) {
                    //已出售
                    mBinding.tvMessagetype.setVisibility(View.GONE);
                    mBinding.llBottom.setVisibility(View.GONE);
                }
                if (publishPatentDetailEntity.getImg() == null || publishPatentDetailEntity.getImg().equals("")) {
                    switch (publishPatentDetailEntity.getCategoryName()) {
                        case "发明专利":
                            mBinding.ivFragmentPatentPublishUploadPicture.setImageResource(R.mipmap.qiyefuwu_module_famingzhuanli_for_patent_publish);
                            break;
                        case "实用新型":
                            mBinding.ivFragmentPatentPublishUploadPicture.setImageResource(R.mipmap.shiyongxinxing_patent_publish);
                            break;
                        case "外观设计":
                            mBinding.ivFragmentPatentPublishUploadPicture.setImageResource(R.mipmap.waiguansheji_patent_publish);
                            break;
                    }
                } else {
                    Glide.with(getContext()).load(publishPatentDetailEntity.getImg()).apply(new RequestOptions()
                            .placeholder(R.mipmap.morentu_change)
                            .fallback(R.mipmap.morentu_change)
                            .error(R.mipmap.morentu_change)).into(mBinding.ivFragmentPatentPublishUploadPicture);
                }
            }
        });

        mViewModel.mJump.observe(this.getViewLifecycleOwner(), integer -> {
            if (integer == 2) {
                ActivityJump.jumpToActivityForResult(getActivity(), MakeOverPublishActivity.class, 1, Pair.create("type", integer), Pair.create("entity", mEntity));
            }
        });

        mViewModel.mDeleteSecess_Type.observe(getViewLifecycleOwner(), new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                getActivity().finish();
                ;
            }
        });

        mBinding.tvMessagetype.setOnClickListener(View -> {
            DialogHelper.showDialog(getActivity(), "温馨提示", "是否确认下架？", "确定", new DialogFunction() {
                @Override
                public void normalRoundDialog() {
                    mViewModel.changeStateMakeoverPublish(PatentAlreadyPublishFragment.this.getActivity(), mBinding.getC().getId(), 2, "0");
                }
            });
        });

        mBinding.tvShangj.setOnClickListener(View -> {
            DialogHelper.showDialog(getActivity(), "温馨提示", "是否确认上架？", "确定", new DialogFunction() {
                @Override
                public void normalRoundDialog() {
                    mViewModel.changeStateMakeoverPublish(PatentAlreadyPublishFragment.this.getActivity(), mBinding.getC().getId(), 2, "1");
                }
            });
        });
        mBinding.tvDelete.setOnClickListener(v -> {
            DialogHelper.showDialog(getActivity(), "温馨提示", "是否确认删除？", "确定", new DialogFunction() {
                @Override
                public void normalRoundDialog() {
                    mViewModel.deleteMakeoverPublish(PatentAlreadyPublishFragment.this.getActivity(), mBinding.getC().getId() + "", 2);
                }
            });
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 0) {
            getActivity().finish();
        }
    }
}
